﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Core.Views;
using Microsoft.Practices.Unity;
namespace Core.Presenters
{
    public class OwnedBuildingsPresenter
    {
        private IOwnedBuildingDAO _ownedBuildingDAO;
        private List<IOwnedBuilding> _districts = new List<IOwnedBuilding>();
        private IAbstractDataView _view;
        public OwnedBuildingsPresenter(IAbstractDataView view)
        {
            _ownedBuildingDAO = ResolverContainer.Resolver.Resolve<IOwnedBuildingDAO>();
            view.AddRecord += AddRecord;
            view.Refreh += Refreh;
            view.DeleteRecord += DeletRecord;
            view.UpdateRecord += Updaterecord;
            _districts = _ownedBuildingDAO.GetAll(true);
            view.Data.DataSource = _districts;
            _view = view;
            _view.ViewName = "Промотр приватезированных зданий";
            view.Show();
        }

        private void Updaterecord(object sender, EventArgs eventArgs)
        {
            new OwnedBuildingPresenter(new OwnedBuildingView(), (IOwnedBuilding)_view.Data.Current);
            Refreh(this, null);
        }

        private void AddRecord(object sender, EventArgs eventArgs)
        {
            
        }

        private void DeletRecord(object sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = _view.Data.Current;
                _ownedBuildingDAO.Delete((IDomainObject)current);
                Refreh(this, null);
            }
        }

        private void Refreh(object sender, EventArgs eventArgs)
        {
            _districts = _ownedBuildingDAO.GetAll(true);
            _view.Data.DataSource = _districts;
        }
    }
}
